var network = require('../../../utils/network_util.js');
var utils = require('../../../utils/utils.js');
var api = require('../../../utils/api.js');
var Zan = require('../../../dist/index');
const app = getApp();
var start = 0;
Page(Object.assign({}, Zan.TopTips, {

  /**
   * 页面的初始数据
   */
  data: {
    record: [],
    userinfo: {},
    expIntoDateStr: 15,
    value2: 1,
    nodata: {
      msg: '没有提现记录',
      notype: 2,
      imgurl: '../../../images/nodatas.png',
      nourl: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(app.globalData.userAndTenantInfo)
    if (app.globalData.userId) {
      this.setData({
        userinfo: app.globalData.userAndTenantInfo
      })
    } else {
      // 由于 openId 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userAndTenantInfoCallback = res => {
        this.setData({
          userinfo: app.globalData.userAndTenantInfo
        })

      }
    }
    this.setData({
      expIntoDateStr: options.expIntoDateStr,
      value2: options.value2,
      dqyongjin: options.dqyongjin
    })
    network._post_page_json(this, api.withdrawalsRecord, start, {}, success);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    start = 0;
    wx.stopPullDownRefresh();
    this.setData({
      record: [],
    });
    network._post_page_json(this, api.withdrawalsRecord, start, {}, success);

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  tixianSq: function (e) {
    if (app.globalData.userAndTenantInfo.mobile) {
      wx.navigateTo({
        url: '/pages/myself/commission/commtixian?expIntoDateStr=' + this.data.expIntoDateStr + '&value2=' + this.data.value2 
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '还未绑定手机号，请绑定手机号',
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/myself/info/bindMobile',
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }

  }
}))

// 提现到由记录
var success = function (that, res) {
  if (res.data.data) {
    var records = res.data.data;
    var sumamount = 0;
    for (var i = 0; i < records.length; i++) {
      records[i].tradeDate = utils.formatDate(new Date(records[i].tradeDate));
      sumamount += records[i].amount
    }
    that.setData({
      sumamount: sumamount,
      record: that.data.record.concat(records),
      recordsTotal: res.data.recordsTotal
    })
  }
}

// var apply = function (mobile) {
//   if (mobile == null) {
//     wx.showModal({
//       title: '提示',
//       content: '还未绑定手机号，请绑定手机号',
//       success: function (res) {
//         if (res.confirm) {
//           wx.navigateTo({
//             url: '/pages/myself/info/bindMobile',
//           })
//         } else if (res.cancel) {
//           console.log('用户点击取消')
//         }
//       }
//     })
//   } else {
//     wx.navigateTo({
//       url: '/pages/myself/commission/commtixian',
//     })
//   }
// }